<?php
	class Default_Model_DbTable_IpBlocking extends Zend_Db_Table_Abstract
	{
	    protected $_name = 'blocked_ips';
	    protected $_sequence = '_seq_blocked_ips';

	    protected $_primary = 'id';
	    protected $_pk = 'id';


	    public function getRow($id, $returnArray = false)
	    {
	        $id = (int)$id;
	    	$row = $this->fetchRow($this->_pk .'= '. $id);
	        if (!$row) {
	            throw new Exception("Count not find row $id");
	        }
	        if ($returnArray == true) {
		        return $row->toArray();
	        }
	        return $row;

	    }

	    public function getRowByIpNumber($ipNumber, $datetime='', $returnArray = false)
	    {

	    	$filter = $this->select()->where('ip_number = ?', $ipNumber);
	    	if (!empty($datetime)) {
	    		$filter->where('end_datetime > ?', $datetime);
	    	}
	    	//echo $filter;
	    	$row = $this->fetchRow($filter);
	        if (!$row) {
	            //throw new Exception("Count not find row with ip number: ".$ipNumber."");
	            return false;
	        }

	        if ($returnArray == true) {
		        return $row->toArray();
	        }
	        return $row;

	    }

	    public function getRows()
	    {
	        $rowsTemp = $this->fetchAll();
	        if (!$rowsTemp) {
	            throw new Exception("Could not find rows");
	        }
	        $pKey = $this->_pk;
	        foreach ($rowsTemp as $key=>$values) {
	        	$rows[$values->$pKey] = (object)$values->toArray();
	        }
	        return $rows;

	    }

	    public function addRow($data)
	    {

	        $this->insert($data);
	        $id = $this->_db->lastInsertId($this->_name);
	        return $id;
	    }

	    public function updateRow($id, $data)
	    {
	        $this->update($data, $this->_pk . ' = ' . (int)$id);

	    }

	    public function deleteRow($id)
	    {
	        $this->delete($this->_pk .'=' . (int)$id);
	    }

	}